package com.mmc.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.mmc.common.result.Result;
import com.mmc.entity.User;
import com.mmc.service.AuthService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import java.util.Map;

/**
 * 认证控制器
 *
 * @author kunfe
 * @since 2024-01-01
 */
@Tag(name = "认证管理", description = "用户登录、登出、获取用户信息等")
@RestController
@RequestMapping("/api/auth")
@RequiredArgsConstructor
@Validated
public class AuthController {

    private final AuthService authService;

    @Operation(summary = "用户登录")
    @SaIgnore
    @PostMapping("/login")
    public Result<Map<String, Object>> login(@Valid @RequestBody LoginRequest request) {
        return Result.ok(authService.login(request));
    }

    @Operation(summary = "用户登出")
    @PostMapping("/logout")
    public Result<Void> logout() {
        authService.logout();
        return Result.ok("登出成功");
    }

    @Operation(summary = "获取当前用户信息")
    @GetMapping("/user-info")
    public Result<User> getUserInfo() {
        Long userId = StpUtil.getLoginIdAsLong();
        User user = authService.getUserInfo(userId);
        return Result.ok(user);
    }

    @Operation(summary = "获取用户菜单")
    @GetMapping("/user-menus")
    public Result<Object> getUserMenus() {
        Long userId = StpUtil.getLoginIdAsLong();
        Object menus = authService.getUserMenus(userId);
        return Result.ok(menus);
    }

    @Operation(summary = "获取用户权限")
    @GetMapping("/user-permissions")
    public Result<Object> getUserPermissions() {
        Long userId = StpUtil.getLoginIdAsLong();
        Object permissions = authService.getUserPermissions(userId);
        return Result.ok(permissions);
    }

    @Operation(summary = "刷新Token")
    @PostMapping("/refresh-token")
    public Result<Map<String, Object>> refreshToken() {
        return Result.ok(authService.refreshToken());
    }

    // 验证码相关接口已移至 CaptchaController

    /**
     * 登录请求
     */
    public static class LoginRequest {
        @Parameter(description = "用户名", required = true)
        @NotBlank(message = "用户名不能为空")
        private String username;

        @Parameter(description = "密码", required = true)
        @NotBlank(message = "密码不能为空")
        private String password;

        @Parameter(description = "验证码")
        private String captchaCode;

        @Parameter(description = "验证码ID")
        private String captchaId;

        @Parameter(description = "记住我")
        private Boolean rememberMe = false;

        // getter and setter
        public String getUsername() { return username; }
        public void setUsername(String username) { this.username = username; }
        public String getPassword() { return password; }
        public void setPassword(String password) { this.password = password; }
        public String getCaptchaCode() { return captchaCode; }
        public void setCaptchaCode(String captchaCode) { this.captchaCode = captchaCode; }
        public String getCaptchaId() { return captchaId; }
        public void setCaptchaId(String captchaId) { this.captchaId = captchaId; }
        public Boolean getRememberMe() { return rememberMe; }
        public void setRememberMe(Boolean rememberMe) { this.rememberMe = rememberMe; }
    }


} 